import moment from 'moment-timezone'
import {ACCESS_TOKEN} from '@/store/mutation-types';

moment.tz.setDefault("Asia/Shanghai");

const EduMixin  =   {
    data() {
        return {
            // 加载数据方法 必须为 Promise 对象
            __loadData: (fn, parameter, callback) => {
                return fn(Object.assign(parameter, this.queryParam))
                    .then(res => {
                        callback(res);
                        return {
                            pageNo: parameter.pageNo,
                            pageSize: parameter.pageSize,
                            totalCount: res.count,
                            totalPage: res.count / parameter.pageSize,
                            data: res.data
                        }
                    });
            },
        };
    },
    
    methods: {
        /**
         * 设置时间组件可选范围
         */
        __disabledDateBeforeCurrent(current) {
            // Can not select days before today and today
            return current && current < moment().endOf('day');
        },
    
        /**
         * moment实例
         * @param args
         * @returns {*|moment.Moment}
         * @private
         */
        __momentInstance(...args) {
            return moment(...args);
        },
        
        /**
         * 路由参数
         * @param key
         * @returns {*}
         * @private
         */
        __getParam(key) {
            return this.$route.params[key];
        },
        
        /**
         * 路由query
         * @param key
         * @returns {*}
         * @private
         */
        __getQuery(key) {
            return this.$route.query[key];
        },
        
        /**
         *  异常提示
         * @param content
         * @param title
         * @private
         */
        __alertResponseException(content, title = '错误提示') {
            this.$notification.error({
                message: title,
                description: content,
                duration: 4
            });
        },
        
        /**
         * 获取当前登录用户ID
         * @returns {number}
         * @private
         */
        __getCurrentUserPk () {
            return 1;
        },
        
        /**
         * @private
         */
        __init_detail (id) {},
        
        /**
         * 跳转
         * @param route
         * @private
         */
        __redirectTo (route) {
            return this.$router.push(route);
        },
        
        /**
         * 获取公共上传请求头
         * @returns {{Authorization: string}}
         * @private
         */
        __systemUploadHeaders() {
            return {
                Authorization: 'Bearer ' + this.$ls.get(ACCESS_TOKEN)
            }
        },
    },
    
    watch: {
        '$route.params.id'(val, old) {
            if( val ) {
                this.id = val;
                this.__init_detail(val);
            }
        }
    }
};

export {EduMixin}